<template>
	<view class="container">
		<image class="logo" src="../../static/logo.png"></image>
		<view class="loginForm">
			<input type="text" v-model="userForm.username" placeholder="请输入账号" />
			<input type="safe-password" v-model="userForm.password" placeholder="请输入密码" />
		</view>
		<button type="primary" style="width: 90%;margin: 40px auto;" @click="goLogin()">登录</button>
	</view>
</template>

<script>
	import AES from "@/common/aes.js"
	import {
		createUuid
	} from "@/common/common.js"
	import {
		oauths,
		oauths2
	} from "@/api/login.js"
	import {
		getUserInfo
	} from "@/api/home.js"
	export default {
		data() {
			return {
				userForm: {
					// // #ifdef APP
					// username: '17640670984',
					// password: 'Ag000000@',
					// // #endif
					// // #ifdef H5 || MP-WEIXIN
					// username: '15084087871',
					// password: 'wy000000',
					// // #endif
					// #ifdef APP
					username: '',
					password: '',
					// #endif
					// #ifdef H5 || MP-WEIXIN
					username: '',
					password: '',
					// #endif
				},
			}
		},
		onShow() {},
		watch: {},
		mounted() {
			this.Uuid = createUuid();
		},
		methods: {
			goLogin() {
				console.log('yidianji')
				var data = this.userForm
				var key = "aOdkcJIlApsOsidK";
				var username = AES.encrypt(this.userForm.username, key)
				var pwd = AES.encrypt(this.userForm.password, key)
				uni.setStorageSync("username", this.userForm.username)
				// #ifdef APP
				console.log(111111111)
				oauths({
					name: username,
					password: pwd
				}).then(res => {
					console.log(3333)
					console.log(res)
					if (res.code == 200) {
						uni.setStorageSync("token", res.data.token)
						uni.switchTab({
							url: "/pages/home/home"
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				}).catch(err => {
					console.log(444)
					uni.stopPullDownRefresh();
				})
				// #endif
				// #ifdef H5 || MP-WEIXIN
				console.log(222222222222222)
				oauths2({
					username: username,
					password: pwd
				}).then(res => {
					if (res.code == 200) {
						uni.setStorageSync("token", res.data.token)
						uni.setStorageSync("ownerId", res.data.ownerId)
						if (res.data.ownerId) {
							this.getUser(res.data.ownerId);
						}
						uni.switchTab({
							url: "/pages/home/home"
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				}).catch(err => {
					uni.stopPullDownRefresh();
				})
				// #endif

			},
			getUser(ownerId) {
				getUserInfo({
					id: ownerId
				}).then(res => {
					// debugger
					if(res.code == 200){
						if(res.rows && res.rows.length > 0){
							var dict = res.rows[0];
							var userName = dict.userName;
							var phonenumber = dict.phonenumber;
							var resourceName = dict.resourceName;
							uni.setStorageSync("userName",userName);
							uni.setStorageSync("phonenumber",phonenumber);
							uni.setStorageSync("resourceName",resourceName);
						}
					}
				}).catch(err => {
					// debugger
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		overflow: hidden;
	}

	.logo {
		width: 80px;
		height: 80px;
		display: block;
		margin: 30px auto;
	}

	.loginForm {
		width: 90%;
		margin: 0 auto;

		uni-input {
			width: 100%;
			overflow: hidden;
			border-bottom: 1px solid #ccc;
			margin-top: 30px;
			height: 40px;
			line-height: 40px;
		}
	}

	input:-internal-autofill-selected {
		-webkit-appearance: menulist-button;
		background-color: rgb(0, 255, 127) !important;
		background-image: none !important;
		color: -internal-light-dark-color(black, white) !important;
	}
</style>